<template>
  <div class="evaluation-container">
    <div class="content-card">
      <header class="header">
        <h1 class="page-title">测评管理</h1>
        <el-button type="primary" @click="openDialog" size="large">修改团队</el-button>
      </header>

      <main>
        <section class="team-section">
          <h2 class="section-title">当前使用docker测评团队</h2>
          <div class="table-container">
            <el-table :data="teams" stripe v-if="teams.length" style="width: 100%">
              <el-table-column prop="name" label="团队名称"></el-table-column>
              <el-table-column prop="ID" label="团队ID" width="180"></el-table-column>
            </el-table>
            <div v-else class="empty-state">
              <p>暂无团队数据</p>
            </div>
          </div>
        </section>
      </main>
    </div>

    <el-dialog
      title="修改测评团队"
      v-model="dialogVisible"
      width="600px"
      :before-close="handleClose"
      top="15vh"
      class="custom-dialog"
    >
      <div class="dialog-content">
        <el-select
          v-model="selectedTeamIds"
          multiple
          filterable
          remote
          :remote-method="searchTeams"
          placeholder="请搜索并选择团队"
          style="width: 100%"
          :loading="loading"
          size="large"
        >
          <el-option
            v-for="item in allTeams"
            :key="item.ID"
            :label="item.name"
            :value="item.ID!"
          >
          </el-option>
        </el-select>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="isSaving"
            >确 定</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import Studycodemodel from "../../../mysql_interface/studycodemodel";
import {
  asyncchangedockerteamid,
  asyncselectdockerteamid,
  asyncselectteam,
} from "@/api";
import {
  ElButton,
  ElDialog,
  ElSelect,
  ElOption,
  ElTable,
  ElTableColumn,
  ElMessage,
} from "element-plus";

const teams = ref<Studycodemodel.Iteam[]>([]);
const allTeams = ref<Studycodemodel.Iteam[]>([]);
const selectedTeamIds = ref<number[]>([]);
const isSaving = ref(false);
const dialogVisible = ref(false);
const loading = ref(false);

onMounted(() => {
  start();
});

async function start(): Promise<void> {
  try {
    const teamids = (await asyncselectdockerteamid()) as number[];
    selectedTeamIds.value = teamids;
    if (teamids.length > 0) {
      const { rows } = await asyncselectteam({ ID: teamids });
      teams.value = rows ?? [];
    } else {
      teams.value = [];
    }
  } catch (error) {
    ElMessage.error("加载团队数据失败");
    console.error(error);
  }
}

async function fetchAllTeams(force = false): Promise<void> {
  if (allTeams.value.length > 0 && !force) return;
  try {
    const { rows } = await asyncselectteam({});
    allTeams.value = rows ?? [];
  } catch (error) {
    ElMessage.error("获取所有团队列表失败");
    console.error(error);
  }
}

async function openDialog() {
  selectedTeamIds.value = teams.value.map((item) => item.ID!);
  await fetchAllTeams(true); // Force fetch all teams when dialog opens
  dialogVisible.value = true;
}

async function searchTeams(query: string) {
  if (query) {
    loading.value = true;
    try {
      const { rows } = await asyncselectteam({
        _like: ["name", query],
      });
      allTeams.value = rows ?? [];
    } catch (error) {
      ElMessage.error("搜索团队失败");
      console.error(error);
    } finally {
      loading.value = false;
    }
  } else {
    await fetchAllTeams(true);
  }
}

function handleClose() {
  dialogVisible.value = false;
}

async function handleConfirm() {
  isSaving.value = true;
  try {
    await asyncchangedockerteamid({ teamid: selectedTeamIds.value });
    ElMessage.success("修改成功");
    await start();
    dialogVisible.value = false;
  } catch (error) {
    ElMessage.error("修改失败");
    console.error(error);
  } finally {
    isSaving.value = false;
  }
}
</script>

<style scoped>
.evaluation-container {
  background-color: #f9fafb;
  min-height: 100vh;
  padding: 2rem;
}

.content-card {
  max-width: 1024px;
  margin: 0 auto;
  background-color: #ffffff;
  border-radius: 0.5rem;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -2px rgba(0, 0, 0, 0.05);
  padding: 1.5rem;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #e5e7eb;
}

.page-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0;
}

.section-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: #374151;
}

.table-container {
  border: 1px solid #e5e7eb;
  border-radius: 0.5rem;
  overflow: hidden;
}

.empty-state {
  text-align: center;
  padding: 2.5rem;
  color: #6b7280;
  background-color: #f9fafb;
}

.dialog-content {
  padding: 1rem;
}

.dialog-footer {
  padding: 1rem;
  background-color: #f9fafb;
  border-bottom-left-radius: 0.5rem;
  border-bottom-right-radius: 0.5rem;
  text-align: right;
}
</style>


